package com.github.iot.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.UserToken;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.DateUtils;
import com.github.iot.utils.ToastUtils;
import com.github.iot.view.RxTitle;

import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 用户授权Activity
 *
 * @Author: 梁声洪
 * @Date: 2020/01/12 9:14
 * @Version: v1.0.0
 */
public class UserTokenActivity extends AppCompatActivity {

    @BindView(R.id.rx_title)
    RxTitle mTitle;

    @BindView(R.id.et_date)
    AppCompatEditText mDate;

    @BindView(R.id.et_token)
    AppCompatEditText mToken;

    private UserToken userToken;
    private CommonDaoUtils<UserToken> userTokenDaoUtils = DaoUtilsStore.getInstance().getUserTokenDaoUtils();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_user_token);

        ButterKnife.bind(this);

        initView();
        initData();
    }

    private void initView() {
        mTitle.setLeftFinish(this);

        mDate.setFocusable(false);
        mDate.setFocusableInTouchMode(false);
    }

    private void initData() {
        List<UserToken> list = userTokenDaoUtils.queryAll();
        if (list.size() > 0) {
            userToken = list.get(0);

            mDate.setText(userToken.getDate());
            mToken.setText(userToken.getToken());
        }
    }

    @OnClick({R.id.et_date, R.id.btn_ok})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.et_date:
                final Calendar calendar = Calendar.getInstance();
                DatePickerDialog dialog = new DatePickerDialog(UserTokenActivity.this,
                        (view, year, month, dayOfMonth) -> {
                            calendar.set(Calendar.YEAR, year);
                            calendar.set(Calendar.MONTH, month);
                            calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                            mDate.setText(DateUtils.date2String(calendar.getTime(), DateUtils.YMD_FORMAT));
                        },
                        calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DAY_OF_MONTH));
                dialog.show();
                break;
            case R.id.btn_ok:
                operationToken();
                break;
        }
    }

    private void operationToken() {
        String date = mDate.getText().toString();
        String token = mToken.getText().toString();

        if (TextUtils.isEmpty(date)) {
            ToastUtils.warning("请选择过期时间！");
            return;
        }

        if (TextUtils.isEmpty(token)) {
            ToastUtils.warning("请输入App授权码！");
            return;
        }

        if (userToken == null) {
            userToken = new UserToken();
            userToken.setDate(date);
            userToken.setToken(token);
            long i = userTokenDaoUtils.insert(userToken);
            if (i > 0) {
                ToastUtils.success("操作成功！");
            } else {
                ToastUtils.error("操作失败");
            }
        } else {
            userToken.setDate(date);
            userToken.setToken(token);
            boolean flag = userTokenDaoUtils.update(userToken);
            if (flag) {
                ToastUtils.success("操作成功！");
            } else {
                ToastUtils.error("操作失败");
            }
        }
    }

}